package com.ltmall.api.weixin.util;

import com.ltmall.api.redis.RedisUtil;
import com.ltmall.api.util.HttpRequestUtil;
import com.wuwangkeji.homeflavor.comm.Path;
import net.sf.json.JSONObject;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TemplateMessageUtil {

    public static boolean sendRegisterSuccessMessage(String openId, int userId, int userCount) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time = sdf.format(new Date());

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("first",
            TemplateMessageUtil.getSimpleMap("亲，恭喜您成为乡味汇第【" + userId + "】位会员！", "#173177"));
        paramsMap.put("keyword1", TemplateMessageUtil.getSimpleMap(userId + "", "#173177"));
        paramsMap.put("keyword2", TemplateMessageUtil.getSimpleMap(time, "#173177"));
        paramsMap.put("remark", TemplateMessageUtil.getSimpleMap(
            "足不出户，尝遍各地特色美食！乡味汇上线雁臻直供新功能，山海生鲜、时令果蔬，优选精品特色美食直供小区。添加微信好友13025030991，随时了解雁臻直供资讯。",
            "#173177"));
        String paramsString = TemplateMessageUtil.getParamsString(openId,
            Constant.getTemplateParams()[0], Path.wechatMallHost + "/?#/upgrade", paramsMap);
        return sendMessage(paramsString);
    }

    public static boolean sendFansOrderCommitMessage(String openId, String goodsTitle,
                                                     BigDecimal income) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time = sdf.format(new Date());

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("first", TemplateMessageUtil.getSimpleMap("亲，您的粉丝已下单，您将获得乡情红包！", "#173177"));
        paramsMap.put("keyword1", TemplateMessageUtil.getSimpleMap(goodsTitle, "#173177"));
        paramsMap.put("keyword2", TemplateMessageUtil.getSimpleMap(income.toString(), "#173177"));
        paramsMap.put("keyword3", TemplateMessageUtil.getSimpleMap(time, "#173177"));
        paramsMap.put("keyword4", TemplateMessageUtil.getSimpleMap("下单成功", "#173177"));
        paramsMap.put("remark",
            TemplateMessageUtil.getSimpleMap("您可以进入“我的”个人中心查看更多佣金详情。", "#173177"));
        String paramsString = TemplateMessageUtil.getParamsString(openId,
            Constant.getTemplateParams()[1], "", paramsMap);
        return sendMessage(paramsString);
    }

    public static boolean sendLevelUpMessage(String openId, String userName, String levelName) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time = sdf.format(new Date());

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("first", TemplateMessageUtil.getSimpleMap("恭喜！您的等级升级了！", "#173177"));
        paramsMap.put("keyword1", TemplateMessageUtil.getSimpleMap(userName, "#173177"));
        paramsMap
            .put("keyword2", TemplateMessageUtil.getSimpleMap(levelName.toString(), "#173177"));
        paramsMap.put("keyword3", TemplateMessageUtil.getSimpleMap(time, "#173177"));
        paramsMap.put("remark",
            TemplateMessageUtil.getSimpleMap("您将获得更多特权，详情查看乡味汇会员优惠政策！", "#173177"));
        String paramsString = TemplateMessageUtil.getParamsString(openId,
            Constant.getTemplateParams()[2], "", paramsMap);
        return sendMessage(paramsString);
    }

    public static boolean sendHasNewFansMessage(String openId, String fansName) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time = sdf.format(new Date());

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("first", TemplateMessageUtil.getSimpleMap("亲，您有新粉丝加入乡味汇，成为您的粉丝！", "#173177"));
        paramsMap.put("keyword1", TemplateMessageUtil.getSimpleMap(fansName, "#173177"));
        paramsMap.put("keyword2", TemplateMessageUtil.getSimpleMap(time, "#173177"));
        paramsMap.put("remark",
            TemplateMessageUtil.getSimpleMap("记得多多关心他，协助他早日成为皇冠代言人！", "#173177"));
        String paramsString = TemplateMessageUtil.getParamsString(openId,
            Constant.getTemplateParams()[3], "", paramsMap);
        return sendMessage(paramsString);
    }

    public static boolean sendBeginDeliveryMessage(String openId, String orderNo,
                                                   String expressName, String expressNo) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("first", TemplateMessageUtil.getSimpleMap("亲，宝贝已经启程了，好想快点来到您身边！", "#173177"));
        paramsMap.put("keyword1", TemplateMessageUtil.getSimpleMap(orderNo, "#173177"));
        paramsMap.put("keyword2",
            TemplateMessageUtil.getSimpleMap(expressName.toString(), "#173177"));
        paramsMap.put("keyword3", TemplateMessageUtil.getSimpleMap(expressNo, "#173177"));
        String paramsString = TemplateMessageUtil.getParamsString(openId,
            Constant.getTemplateParams()[4], Path.wechatMallHost + "/?#/logistics/" + orderNo,
            paramsMap);
        return sendMessage(paramsString);
    }

    public static boolean sendFansLevelUpIncomeMessage(String openId, String userName,
                                                       BigDecimal income) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time = sdf.format(new Date());

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("first", TemplateMessageUtil.getSimpleMap("亲，上个月您推荐代言人奖已经到账啦！", "#173177"));
        paramsMap.put("keyword1", TemplateMessageUtil.getSimpleMap(userName, "#173177"));
        paramsMap.put("keyword2", TemplateMessageUtil.getSimpleMap(income.toString(), "#173177"));
        paramsMap.put("keyword3", TemplateMessageUtil.getSimpleMap("0", "#173177"));
        paramsMap.put("keyword4", TemplateMessageUtil.getSimpleMap(time, "#173177"));
        String paramsString = TemplateMessageUtil.getParamsString(openId,
            Constant.getTemplateParams()[5], "", paramsMap);
        return sendMessage(paramsString);
    }

    public static boolean sendGroupBuyingSuccessMessage(String openId, String goodsTitle,
                                                        BigDecimal goodsPrice) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("first", TemplateMessageUtil.getSimpleMap("您参加的社区团购已成功，请等待发货", "#173177"));
        paramsMap.put("keyword1", TemplateMessageUtil.getSimpleMap(goodsTitle, "#173177"));
        paramsMap.put("keyword2",
            TemplateMessageUtil.getSimpleMap(goodsPrice.toString(), "#173177"));
        paramsMap.put("remark",
            TemplateMessageUtil.getSimpleMap("社区团购，省钱更省时间，售后更有保障。感谢您的支持！", "#173177"));
        String paramsString = TemplateMessageUtil.getParamsString(openId,
            Constant.getTemplateParams()[6], "", paramsMap);
        return sendMessage(paramsString);
    }

    public static boolean sendGroupBuyingFailureMessage(String openId, String goodsTitle,
                                                        BigDecimal goodsPrice,
                                                        BigDecimal refundAmount) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("first",
            TemplateMessageUtil.getSimpleMap("您好！您参加的社区团购由于参团人数不足，拼团失败。", "#173177"));
        paramsMap.put("keyword1", TemplateMessageUtil.getSimpleMap(goodsTitle, "#173177"));
        paramsMap.put("keyword2",
            TemplateMessageUtil.getSimpleMap(goodsPrice.toString(), "#173177"));
        paramsMap.put("keyword3",
            TemplateMessageUtil.getSimpleMap(refundAmount.toString(), "#173177"));
        paramsMap
            .put("remark", TemplateMessageUtil.getSimpleMap("您的退款已经提交微信审核，感谢您的参与！", "#173177"));
        String paramsString = TemplateMessageUtil.getParamsString(openId,
            Constant.getTemplateParams()[7], "", paramsMap);
        return sendMessage(paramsString);
    }

    public static boolean sendDirectorPrivilegeOrderIncomeMessage(String openId, Double income) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("first",
            TemplateMessageUtil.getSimpleMap("亲，您推荐的粉丝已申领董事特权，您将获得乡情红包！", "#173177"));
        paramsMap.put("keyword1", TemplateMessageUtil.getSimpleMap(income.toString(), "#173177"));
        paramsMap.put("keyword2", TemplateMessageUtil.getSimpleMap(time, "#173177"));
        paramsMap.put("remark", TemplateMessageUtil.getSimpleMap("分享多多，红包多多！点击查看详情", "#173177"));
        String paramsString = TemplateMessageUtil.getParamsString(openId,
            Constant.getTemplateParams()[8],
            "http://www.xiangweihui.com/wechatPay/?#/recommend-reward", paramsMap);
        return sendMessage(paramsString);
    }

    public static boolean sendBeginSupplyDeliveryMessage(String openId, String goodName) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String deliverTime = sdf.format(new Date());

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("first", TemplateMessageUtil.getSimpleMap("亲，您在乡味汇购买的商品已启程了!", "#173177"));
        paramsMap.put("keyword1", TemplateMessageUtil.getSimpleMap(goodName, "#173177"));
        paramsMap.put("keyword2", TemplateMessageUtil.getSimpleMap(deliverTime, "#173177"));
        paramsMap.put("remark", TemplateMessageUtil.getSimpleMap(
            "温馨提示：货品将尽快送达您的小区提货点，请保持手机畅通，便于联系通知到您。如有其他需要，随时拨打客服热线400-801-8015。", "#173177"));
        String paramsString = TemplateMessageUtil.getParamsString(openId,
            Constant.getTemplateParams()[9], "", paramsMap);
        return sendMessage(paramsString);
    }

    public static boolean sendMessage(String param) {
        //		JSONObject accessTokenJson = HttpRequestUtil.sendGet("https://api.weixin.qq.com/cgi-bin/token", "grant_type=client_credential&appid=" + Constants.OA_APP_ID + "&secret=" + Constants.OA_APP_SECRET);
        //		String accessToken = (String) accessTokenJson.get("access_token");
        String accessToken = RedisUtil.getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
                     + accessToken;
        JSONObject json = HttpRequestUtil.sendPost(url, param);
        if (json.containsKey("errcode")) {
            int errcode = (Integer) json.get("errcode");
            if (errcode > 0) {
                System.out.println("推送模板消息错误：" + errcode);
                return false;
            }
        }

        return true;
    }

    public static String getParamsString(String openId, String templateId, String url,
                                         Map<String, Object> map) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("touser", openId);
        paramsMap.put("template_id", templateId);
        paramsMap.put("url", url);
        paramsMap.put("data", map);
        JSONObject json = new JSONObject();
        json.putAll(paramsMap);
        return json.toString(2);
    }

    public static Map<String, String> getSimpleMap(String value, String color) {
        Map<String, String> paramsMap = new HashMap<String, String>();
        paramsMap.put("value", value);
        paramsMap.put("color", color);
        return paramsMap;
    }

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String deliverTime = sdf.format(new Date());
        System.out.println(deliverTime);
    }
}
